extends Node

func _ready() -> void:
	var s = create_custom_cube_mesh(1)
	ResourceSaver.save(s,"res://a.res")

func create_custom_cube_mesh(size: float = 1.0) -> ArrayMesh:
	var arrays = []
	arrays.resize(Mesh.ARRAY_MAX)
	
	# Define vertices, normals, UVs, and indices for all faces
	var vertices: PackedVector3Array = PackedVector3Array()
	var normals: PackedVector3Array = PackedVector3Array()
	var uvs: PackedVector2Array = PackedVector2Array()
	var indices: PackedInt32Array = PackedInt32Array()
	
	# Half size for centering at origin
	var hs = size / 2.0
	
	# Helper function to add a quad face
	var add_face = func(positions: Array, normal: Vector3, uv_rot: int = 0):
		var base_index = vertices.size()
		
		# Add vertices
		for pos in positions:
			vertices.append(pos)
			normals.append(normal)
		
		# Standard UVs: (0,0), (1,0), (1,1), (0,1)
		# Rotate UVs if needed for orientation (0: default, 1: 90deg CW, 2: 180deg, 3: 270deg CW)
		var uv_list = [
			Vector2(0, 1), Vector2(1, 1), Vector2(1, 0), Vector2(0, 0)
		]
		# Apply rotation if needed (though for reversal, rotation might need adjustment)
		if uv_rot == 1:
			uv_list = [Vector2(0, 0), Vector2(0, 1), Vector2(1, 1), Vector2(1, 0)]
		elif uv_rot == 2:
			uv_list = [Vector2(1, 0), Vector2(0, 0), Vector2(0, 1), Vector2(1, 1)]
		elif uv_rot == 3:
			uv_list = [Vector2(1, 1), Vector2(1, 0), Vector2(0, 0), Vector2(0, 1)]
		for uv in uv_list:
			uvs.append(uv)
		
		# Add indices for two triangles
		indices.append(base_index + 0)
		indices.append(base_index + 3)
		indices.append(base_index + 2)
		indices.append(base_index + 0)
		indices.append(base_index + 2)
		indices.append(base_index + 1)
	
	# Front face (Z positive)
	add_face.call([
		Vector3(-hs, -hs, hs), Vector3(hs, -hs, hs),
		Vector3(hs, hs, hs), Vector3(-hs, hs, hs)
	], Vector3(0, 0, 1), 0)
	
	# Back face (Z negative)
	add_face.call([
		Vector3(hs, -hs, -hs), Vector3(-hs, -hs, -hs),
		Vector3(-hs, hs, -hs), Vector3(hs, hs, -hs)
	], Vector3(0, 0, -1), 0)
	
	# Right face (X positive)
	add_face.call([
		Vector3(hs, -hs, hs), Vector3(hs, -hs, -hs),
		Vector3(hs, hs, -hs), Vector3(hs, hs, hs)
	], Vector3(1, 0, 0), 0)
	
	# Left face (X negative)
	add_face.call([
		Vector3(-hs, -hs, -hs), Vector3(-hs, -hs, hs),
		Vector3(-hs, hs, hs), Vector3(-hs, hs, -hs)
	], Vector3(-1, 0, 0), 0)
	
	# Top face (Y positive)
	add_face.call([
		Vector3(-hs, hs, hs), Vector3(hs, hs, hs),
		Vector3(hs, hs, -hs), Vector3(-hs, hs, -hs)
	], Vector3(0, 1, 0), 0)
	
	# Bottom face (Y negative)
	add_face.call([
		Vector3(-hs, -hs, -hs), Vector3(hs, -hs, -hs),
		Vector3(hs, -hs, hs), Vector3(-hs, -hs, hs)
	], Vector3(0, -1, 0), 0)
	
	# Assign arrays
	arrays[Mesh.ARRAY_VERTEX] = vertices
	arrays[Mesh.ARRAY_NORMAL] = normals
	arrays[Mesh.ARRAY_TEX_UV] = uvs
	arrays[Mesh.ARRAY_INDEX] = indices
	
	# Create the mesh
	var mesh = ArrayMesh.new()
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
	
	return mesh

# Example usage:
# var cube_mesh = create_custom_cube_mesh(2.0)
# var mesh_instance = MeshInstance3D.new()
# mesh_instance.mesh = cube_mesh
# add_child(mesh_instance)
